# -*- coding: utf-8 -*-

import os

from time import *

class DataFile(object):
  
  def __init__(self, filename):
    
    if os.path.exists(filename):
      self.filename = filename
      statinfo = os.stat(self.filename)
      self.size = statinfo[6]
      self.mtime = statinfo[8]
      self.ctime = statinfo[9] # different between unix and windows
    else:
      raise WindowsError("file do not exists")
    #(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, self.st_size, st_atime,  self.st_mtime, self.st_ctime) =  statinfo

  def getShortName(self):
    name = os.path.split(self.filename)
    return name[1]
    
  def getTime(self, type):
    if type == 'm':
      return strftime("%Y-%m-%d %H:%M:%S", localtime(self.mtime))
    elif type == 'c':
      return strftime("%Y-%m-%d %H:%M:%S", localtime(self.ctime))
    else:
      return None

def test():
  print "begin test\n"
  try:
    file = DataFile("F:/dev/excel/0.2/pipe/datafile.py")
  except WindowsError, e:
    print e
    exit(0)
  print file.getShortName()
  print file
  #file.reset("F:/dev/excel/0.2/pipe/main.py")
  #file.reset()
  print file.getShortName()
  print file.size
  #file.st_size = 10
  print file.size
  print file.getTime('m')
  print file.getTime('c')
  #print file.st_mtime
  file = DataFile("F:/dev/excel/0.2/pipe/main.py")
  print file
  print file.filename
  print file.size
  print "\nend test"

if __name__ == "__main__":
    test()          
